﻿#include "globalsettings.h"
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

QColor GlobalSettings::_curveColor[SAMPLE_LCHANNEL_SUM] = {Qt::black,
                                                           Qt::darkGray,
                                                           Qt::gray,
                                                           Qt::lightGray,
                                                           Qt::red,
                                                           Qt::green,
                                                           Qt::blue,
                                                           Qt::cyan,
                                                           Qt::magenta,
                                                           Qt::yellow,
                                                           Qt::darkRed,
                                                           Qt::darkGreen,
                                                           Qt::darkBlue,
                                                           Qt::darkCyan,
                                                           Qt::darkMagenta,
                                                           Qt::darkYellow};

GlobalSettings* GlobalSettings::_pInstance = NULL;

GlobalSettings::GlobalSettings()
{
    _serverIP = "192.168.1.100";
    _serverPort = 9271;

    _tcpClientThread = NULL;
    _frameMgr = NULL;
}

GlobalSettings* GlobalSettings::instance()
{
    if (_pInstance == NULL)
    {
        _pInstance = new GlobalSettings();
        if(!_pInstance->loadFromJson(appDir() + "/config/parameters.json"))
            _pInstance->storeToJson();
    }

    return _pInstance;
}

bool GlobalSettings::loadFromJson(const QString& jsonFile)
{
    //return ret;
}

void GlobalSettings::storeToJson()
{

}

QString GlobalSettings::appDir()
{
    QString appdir = QCoreApplication::applicationDirPath();
#ifdef Q_OS_MACOS
    int index = appdir.lastIndexOf("/");
    appdir = appdir.remove(index,appdir.length() - index);
    index = appdir.lastIndexOf("/");
    appdir = appdir.remove(index,appdir.length() - index);
    index = appdir.lastIndexOf("/");
    appdir = appdir.remove(index,appdir.length() - index);
#endif
    return appdir;
}

QString GlobalSettings::appWorkDir()
{

}

bool GlobalSettings::appendHChannelSample(const unsigned char* buf)
{
    PTR_HSamplePacket samplePacket = new HSamplePacket;
    samplePacket->parser(buf,BYTES_PER_HCHANNEL_SAMPLE);

    _hchannelSamples.push_back(samplePacket);
}

bool GlobalSettings::appendLChannelSample(const unsigned char* buf)
{
    PTR_LSamplePacket samplePacket = new LSamplePacket;
    samplePacket->parser(buf,BYTES_PER_LCHANNEL_SAMPLE);

    _lchannelSamples.push_back(samplePacket);
}

void GlobalSettings::clearSamples()
{
    QVector<PTR_HSamplePacket>::iterator it = _hchannelSamples.begin();
    while(it != _hchannelSamples.end())
    {
        delete *it;
        it++;
    }
    _hchannelSamples.clear();

    QVector<PTR_LSamplePacket>::iterator ip = _lchannelSamples.begin();
    ip = _lchannelSamples.begin();
    while(ip != _lchannelSamples.end())
    {
       delete *ip;
       ip++;
    }
    _lchannelSamples.clear();
}

void GlobalSettings::initChartStyle(QCustomPlot* plot)
{
//    QBrush brush(Qt::white, Qt::SolidPattern);
//    plot->setBackground(brush);
//    plot->xAxis->setBasePen(QPen(QColor(0xe0, 0xe0, 0xe0), 2));
//    plot->yAxis->setBasePen(QPen(QColor(0xe0, 0xe0, 0xe0), 2));
//    plot->yAxis2->setBasePen(QPen(QColor(0xe0, 0xe0, 0xe0), 2));
//    plot->xAxis->setTickPen(QPen(QColor(0xb0, 0xb0, 0xb0), 2));
//    plot->yAxis->setTickPen(QPen(QColor(0xb0, 0xb0, 0xb0), 2));
//    plot->yAxis2->setTickPen(QPen(QColor(0xb0, 0xb0, 0xb0), 2));
//    plot->xAxis->setSubTickPen(QPen(QColor(0xa0, 0xa0, 0xa0), 1));
//    plot->yAxis->setSubTickPen(QPen(QColor(0xa0, 0xa0, 0xa0), 1));
//    plot->yAxis2->setSubTickPen(QPen(QColor(0xa0, 0xa0, 0xa0), 1));
//    plot->xAxis->setTickLabelColor(QColor(0xe0, 0xe0, 0xe0));
//    plot->yAxis->setTickLabelColor(QColor(0xe0, 0xe0, 0xe0));
//    plot->yAxis2->setTickLabelColor(QColor(0xe0, 0xe0, 0xe0));
//    plot->xAxis->setLabelColor(QColor(0xe0, 0xe0, 0xe0));
//    plot->yAxis->setLabelColor(QColor(0xe0, 0xe0, 0xe0));
//    plot->yAxis2->setLabelColor(QColor(0xe0, 0xe0, 0xe0));

    plot->legend->setVisible(true);
    plot->legend->setBrush(QBrush(QColor(255,255,255,150)));
}

void GlobalSettings::initCurveColor(QCPCurve* graph, unsigned int index)
{
    graph->setPen(QPen(_curveColor[index], 1));
}
